//
//  BVClearAlertView.m
//  AssistantCloud1
//
//  Created by YYKJ on 2017/6/25.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVTextFieldAlertView.h"
#import "ZZTextField.h"

#define AlertW [UIScreen mainScreen].bounds.size.width-100
#define AlertH 160

@interface BVTextFieldAlertView ()

@property (nonatomic ,retain) UIView *alertView;

@property (nonatomic ,strong) UIView           *upLine;
@property (nonatomic ,strong) UIView           *bottomLine;
@property (nonatomic ,strong) UIView           *verticalLine;

@property (nonatomic ,strong) UIButton         *upLeftBtn;
@property (nonatomic ,strong) UIButton         *upRightBtn;
@property (nonatomic ,strong) ZZTextField      *tF;

@property (nonatomic ,strong) NSString         *tempStr;

@end

@implementation BVTextFieldAlertView

- (instancetype)init{
    
    if (self = [super init]) {
    
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:0.5];
        
        self.alertView = [[UIView alloc]init];
        [self addSubview:self.alertView];
        [self.alertView setBackgroundColor:[UIColor whiteColor]];
        [self.alertView setFrame:CGRectMake(0, 00, AlertW, AlertH)];
        [self.alertView.layer setCornerRadius:5.0];
        [self.alertView setCenterX:self.center.x];
        [self.alertView setCenterY:self.center.y];
        
        UILabel *lb = [[UILabel alloc]init];
        [self.alertView addSubview:lb];
        [lb setFrame:CGRectMake(0, 0, AlertW, 40)];
        [lb setFont:[UIFont systemFontOfSize:16]];
        [lb setTextAlignment:NSTextAlignmentCenter];
        [lb setTextColor:[UIColor BVTextDarkGrayColor]];
        [lb setText:@"温馨提示"];
        
        self.upLine = [[UIView alloc]initWithFrame:CGRectMake(0,40,AlertW,1.0)];
        self.upLine.backgroundColor = [UIColor BVLineDarkGrayColor];
        [self.alertView addSubview:self.upLine];
        
        self.tF = [[ZZTextField alloc]init];
        [self.alertView addSubview:self.tF];
        [self.tF setFrame:CGRectMake(20, 50, (AlertW)-40, 40)];
        [self.tF.layer setBorderColor:[UIColor BVLineGrayColor].CGColor];
        [self.tF.layer setBorderWidth:1.0];
        [self.tF.layer setMasksToBounds:YES];
        [self.tF.layer setCornerRadius:3.0];
        [self.tF setPlaceholder:@"请输入您的昵称"];
        
        self.bottomLine = [[UIView alloc]initWithFrame:CGRectMake(0,AlertH-40,AlertW,1.0)];
        self.bottomLine.backgroundColor = [UIColor BVLineGrayColor];
        [self.alertView addSubview:self.bottomLine];
        
        self.verticalLine = [[UIView alloc]initWithFrame:CGRectMake((AlertW)/2,AlertH-36,1.0,AlertH-(AlertH-36))];
        self.verticalLine.backgroundColor = [UIColor BVLineGrayColor];
        [self.alertView addSubview:self.verticalLine];
        
        self.upLeftBtn = [self upBtn];
        [self.alertView addSubview:self.upLeftBtn];
        self.upLeftBtn.frame = CGRectMake(00, AlertH-40, 110, 40);
        [self.upLeftBtn setTitle:@"取消" forState:UIControlStateNormal];
        [self.upLeftBtn setTag:100];
        
        self.upRightBtn = [self upBtn];
        [self.alertView addSubview:self.upRightBtn];
        self.upRightBtn.frame = CGRectMake(AlertW-110, AlertH-40, 109, 40);
        [self.upRightBtn setTitle:@"确认" forState:UIControlStateNormal];
        [self.upRightBtn setTag:200];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector
         (keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector
         (keyboardWillChange:) name:UIKeyboardWillHideNotification object:nil];
        
    }
    return self;
}

#pragma mark - 弹出
-(void)showMKPAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}

-(void)creatShowAnimation
{
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];
    
}

-(void)disBtnClick:(UIButton *)btn{
    if (btn.tag == 100) {
        [self removeFromSuperview];
    }else{
        _tempStr = self.tF.text;
        NSLog(@"---%@",_tempStr);
        if (self.AlertResult) {
            self.AlertResult(_tempStr);
            NSLog(@"---%@",_tempStr);
            [self removeFromSuperview];
        }
    }
}

#pragma mark - 当键盘升起或下落时
-(void)keyboardWillChange:(NSNotification *)notification{
    
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    if (notification.name == UIKeyboardWillShowNotification) {
        [UIView animateWithDuration:duration animations:^{
            [self.alertView setCenterY:180];
        }];
    }else{
        [UIView animateWithDuration:duration animations:^{
            [self.alertView setCenterX:self.center.x];
            [self.alertView setCenterY:self.center.y];
        }];
    }
}

-(UIButton *)upBtn{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(disBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [btn setTitleColor:[UIColor BVTextDarkGrayColor]forState:UIControlStateNormal];
    [btn.titleLabel setTextAlignment:NSTextAlignmentCenter];
    return btn;
}

@end
